import { app as n, BrowserWindow as l, shell as c } from "electron";
import { dirname as m, join as t } from "node:path";
import { fileURLToPath as w } from "node:url";
import { existsSync as p } from "node:fs";
const s = process.env.VITE_DEV_SERVER_URL, f = w(import.meta.url), r = m(f);
n.commandLine.appendSwitch("allow-file-access-from-files");
function a() {
  const e = new l({
    width: 1100,
    height: 720,
    show: !1,
    webPreferences: {
      contextIsolation: !0,
      nodeIntegration: !1,
      // 关闭同源策略 & 允许 HTTP 页面请求本地 file:// 资源（开发阶段）
      webSecurity: !1,
      allowRunningInsecureContent: !0,
      preload: t(r, "preload.mjs")
    }
  });
  if (e.once("ready-to-show", () => e.show()), e.webContents.setWindowOpenHandler(({ url: o }) => (c.openExternal(o), { action: "deny" })), s)
    e.loadURL(s), e.webContents.openDevTools({ mode: "detach" });
  else {
    const o = [
      t(r, "../dist/index.html"),
      // asar 内或开发环境
      t(process.resourcesPath, "app.asar/dist/index.html")
      // 备用
    ], i = o.find((d) => p(d));
    i ? e.loadFile(i) : console.error("无法找到 index.html，尝试的路径：", o);
  }
}
n.whenReady().then(() => {
  a(), n.on("activate", () => {
    l.getAllWindows().length === 0 && a();
  });
});
n.on("window-all-closed", () => {
  process.platform !== "darwin" && n.quit();
});
